﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SSOCommon;

namespace SSOServer
{
    public static class StateManager
    {
        private static Dictionary<string, UserInfo> _usersState = new Dictionary<string, UserInfo>();
        private static object _lockKey = new object();

        static StateManager()
        {
        }

        public static void Add(string token, UserInfo userInstance)
        {
            if (String.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }
            if (userInstance == null)
            {
                throw new ArgumentNullException("userInstance");
            }
            lock (_lockKey)
            {
                if (_usersState.ContainsKey(token))
                {
                    return;
                }
                _usersState.Add(token, userInstance);
            }
        }

        public static void Remove(string token)
        {
            if (String.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }
            lock (_lockKey)
            {
                if (_usersState.ContainsKey(token))
                {
                    _usersState.Remove(token);
                }
            }
        }

        public static void Clear()
        {
            lock (_usersState)
            {
                _usersState.Clear();
            }
        }

        public static UserInfo GetUserInfo(string token)
        {
            if (String.IsNullOrEmpty(token))
            {
                return null;
            }
            return _usersState[token];
        }


        public static bool IsExist(string token)
        {
            if (String.IsNullOrEmpty(token))
            {
                return false;
            }
            return _usersState.ContainsKey(token);
        }
    }
}